package com.ruoyi.project.cms.mm.delay.controller;

import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.mm.delay.domain.Delay;
import com.ruoyi.project.cms.mm.delay.serivce.DelayService;
import com.ruoyi.project.cms.mm.task.domain.TaskInfo;
import com.ruoyi.project.cms.mm.task.service.TaskInfoService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import javafx.concurrent.Task;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @create 2020-05-18 14:59
 * @desc 任务延期控制层
 **/
@Controller
@RequestMapping("cms/mm/delay")
public class DelayController extends BaseController {
    @Autowired
    private DelayService delayService;
    @Autowired
    private TaskInfoService taskInfoService;
    @Autowired
    private IUserService userService;

    private String prefix = "cms/mm/delay";

    /**
     * 跳转delay.html页面
     *
     * @return
     */
    @GetMapping()
    public String delay() {
        return prefix + "/delay";
    }

    /**
     * 查询多个延期申请
     *
     * @param delay
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Delay delay) {
        //我自己只能看到申请人是自己的延期申请
        System.out.println("进入查询方法");
        //获取当前登录的用户,对应的就是数据库表中的uid字段
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        delay.setClogname(user.getLoginName());
        //开启分页
        startPage();
        List<Delay> list = delayService.selectMoreDelays(delay);
        return getDataTable(list);
    }

    //跳转新增页面
    @GetMapping("/addDelay")
    public String add(ModelMap map) {
        System.out.println("开始跳转");
        //在跳转之前，需要根据当前登录的用户，查询当前用户的任务，也就是查询do_uid为当前任务的项目
        User user = (User) SecurityUtils.getSubject().getPrincipal();
//        根据user_id查询任务
        List<TaskInfo> list = taskInfoService.selectTaskInfoByDuid(user.getLoginName());
        System.out.println(list);
        map.put("taskList", list);
        map.put("user", user);
        return prefix + "/addDelay";
    }

    @PostMapping("/doAdd")
    @ResponseBody
    public AjaxResult doAdd(Delay delay) {
        System.out.println("执行任务添加操作");
        System.out.println(delay);
        TaskInfo info = taskInfoService.selectTaskInfoByTid(delay.getTid());
        delay.setNextauditor(info.getClogname());
        //创建日期、状态,设置状态为0新建
        delay.setStatus(0);
        Date date = new Date();
        delay.setCdate(date);
        return toAjax(delayService.insert(delay));
    }

    //跳转延期审批页面，这个页面当中只能看到nextauditor=当前用户
    @GetMapping("/delayApprv")
    public String delayApprv() {
        System.out.println("跳转延期审批页面");
        return prefix + "/delayApprv";
    }

    //    查询nextauditor是当前用户且审批未完成的所有信息
    @PostMapping("/apprvlist")
    @ResponseBody
    public TableDataInfo apprvlist(Delay delay) {
        //我自己只能看到申请人是自己的延期申请
        System.out.println("进入审批查询方法");
        //获取当前登录的用户
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        delay.setNextauditor(user.getLoginName());
        User user1 = userService.selectUserByLoginName(delay.getNextauditor());
        delay.setCreatUser(user);
        delay.setApprvUser(user1);
        //开启分页
        startPage();
        List<Delay> list = delayService.selectDelaysByNextAndStatus(delay);
        return getDataTable(list);
    }

    //删除审批
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        System.out.println("开始文件删除操作");
        try {
            return toAjax(delayService.deleteDelayBydid(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    //    打开编辑页面
    @GetMapping("/edit/{did}")
    public String edit(@PathVariable("did") Integer did, ModelMap mmap) {
        System.out.println("打开编辑页面");
        mmap.put("delay", delayService.selectDelayByDid(did));
        return prefix + "/editDelay";
    }

    //    进行编辑操作
    @PostMapping("/editDelay")
    @ResponseBody
    public AjaxResult editDelay(Delay delay) {
        System.out.println("开始执行编辑操作");
        System.out.println(delay);
        return toAjax(delayService.updateDelay(delay));
    }

    //跳转详情页面
    @GetMapping("detail/{did}")
    public String detail(@PathVariable Integer did, ModelMap map) {
        System.out.println("111");
        Delay delay = delayService.selectDelayDetailByDid(did);
        System.out.println(delay);
        map.put("delay", delay);
        return prefix + "/delayDetail";
    }

    //    打开审批页面
    @GetMapping("/apprv/{did}")
    public String apprv(@PathVariable("did") Integer did, ModelMap mmap) {
        System.out.println("打开审批对话框");
        Delay delay = delayService.selectDelayByDid(did);
        System.out.println(delay);
        mmap.put("delay", delay);
        return prefix + "/apprvDelay";
    }

    //    执行审批操作
    @PostMapping("/doApprv")
    @ResponseBody
    public AjaxResult doApprv(Delay delay) {
//        读取前台传过来的信息
        System.out.println("执行审批操作");
//        设置当前时间为审批时间
        delay.setAdate(new Date());
        return toAjax(delayService.apprvDelay(delay));
    }
}
